<script type="text/javascript">
$(document).ready(function(){
//Initialize all components.
var natureOfBusinessRadio=$('#BusinessClientForm #NatureOfBusinessRadio').buttonset();
var fiscalYearEndContainer=$('#BusinessClientForm #FiscalYearEndContainer').hide();
var expiryDate=$('#BusinessClientForm #ExpiryDate').datepicker({dateFormat:'MM dd, yy'});
var fiscalYearEnd=$('#BusinessClientForm #FiscalYearEnd').datepicker({dateFormat:'MM dd, yy'});
var statusRadio=$('#BusinessClientForm #StatusRadio').buttonset();
var effectiveDateContainer=$('#BusinessClientForm #EffectiveDateContainer').hide();
var effectiveDate=$('#BusinessClientForm #EffectiveDate').datepicker({dateFormat:'MM dd, yy'});
var including=$('#BusinessClientForm #Including').buttonset();
var typeOfServicesContainer=$('#BusinessClientForm #TypeOfServicesContainer').buttonset();
var businessNumberConsentForm=$('#BusinessClientForm #BusinessNumberConsentForm').button();
var WSIBNumberConsentForm=$('#BusinessClientForm #WSIBNumberConsentForm').button();
var EHTNumberConsentForm=$('#BusinessClientForm #EHTNumberConsentForm').button();
$('#BusinessClientForm #PickupDate').spinner({min:1,max:31});
$('#BusinessClientForm #NoLongerOurClient').click(function(){
	effectiveDateContainer.show();
});
$('#BusinessClientForm #Corporation').click(function(){
	fiscalYearEndContainer.show();
});
$('#BusinessClientForm #Partnership').click(function(){
	fiscalYearEndContainer.hide();
	$('#BusinessClientForm #FiscalYearEnd').clear();
});
$('#BusinessClientForm #Sole-Proprietor').click(function(){
	fiscalYearEndContainer.hide();
	$('#BusinessClientForm #FiscalYearEnd').clear();
});
$('#BusinessClientForm #Active').click(function(){
	effectiveDateContainer.hide();
	$('#BusinessClientForm #EffectiveDate').clear();
});
// Initialize all Dialogs and hide them.
var createBusinessClientDialog=$('#BusinessClientDialog').dialog({
	autoOpen:false,
	title:"Business Client",
	modal:true,
	resizable:false,
	width:1200,
	height:850,
	buttons:{
		"Save":function(){
			var client={
				"clientId":$('#clientId').val(),
				"ClientNumber":$('#BusinessClientForm #ClientNumber').val(),
				"Name":$('#BusinessClientForm #Name').val(),
				"BusinessName":$('#BusinessClientForm #BusinessName').val(),
				"ExpiryDate":($('#BusinessClientForm #ExpiryDate').datepicker('getDate')!=undefined?$('#BusinessClientForm #ExpiryDate').datepicker('getDate').getTime():''),
				"RegisteredAddress":$('#BusinessClientForm #RegisteredAddress').val(),
				"BusinessAddress":$('#BusinessClientForm #BusinessAddress').val(),
				"MailingAddress":$('#BusinessClientForm #MailingAddress').val(),
				"ContactPerson":$('#BusinessClientForm #ContactPerson').val(),
				"BusinessPhone":$('#BusinessClientForm #BusinessPhone').val(),
				"CellPhone":$('#BusinessClientForm #CellPhone').val(),
				"HomePhone":$('#BusinessClientForm #HomePhone').val(),
				"Fax":$('#BusinessClientForm #Fax').val(),
				"Email":$('#BusinessClientForm #Email').val(),
				"NatureOfBusiness":$('#BusinessClientForm #NatureOfBusinessRadio :radio:checked').attr('id'),
				"FiscalYearEnd":($('#BusinessClientForm #FiscalYearEnd').datepicker('getDate')!=undefined?$('#BusinessClientForm #FiscalYearEnd').datepicker('getDate').getTime():''),
				"Status":$('#BusinessClientForm #StatusRadio :radio:checked').attr('id'),
				"EffectiveDate":($('#BusinessClientForm #EffectiveDate').datepicker('getDate')!=undefined?$('#BusinessClientForm #EffectiveDate').datepicker('getDate').getTime():''),
				"Article":($('#BusinessClientForm #Article').attr('checked')=='checked'?true:false),
				"Form1":($('#BusinessClientForm #Form1').attr('checked')=='checked'?true:false),
				"HST":($('#BusinessClientForm #HST').attr('checked')=='checked'?true:false),
				"Payroll":($('#BusinessClientForm #Payroll').attr('checked')=='checked'?true:false),
				"Tax":($('#BusinessClientForm #Tax').attr('checked')=='checked'?true:false),
				"ImportExport":($('#BusinessClientForm #ImportExport').attr('checked')=='checked'?true:false),
				"BusinessNumber":($('#BusinessClientForm #BusinessNumber').val()),
				"BusinessNumberConsentForm":($('#BusinessClientForm #BusinessNumberConsentForm').attr('checked')=='checked'?true:false),
				"WSIBNumber":($('#BusinessClientForm #WSIBNumber').val()),
				"WSIBNumberConsentForm":($('#BusinessClientForm #WSIBNumberConsentForm').attr('checked')=='checked'?true:false),
				"EHTNumber":($('#BusinessClientForm #EHTNumber').val()),
				"EHTNumberConsentForm":($('#BusinessClientForm #EHTNumberConsentForm').attr('checked')=='checked'?true:false),
				"Accountant":($('#BusinessClientForm #AccountantContainer :radio:checked').val()),
				"TypeOfServices":$('#BusinessClientForm #TypeOfServicesContainer :radio:checked').attr('id'),
				"PASID":($('#BusinessClientForm #PASID').val()),
				"PickupDate":($('#BusinessClientForm #PickupDate').val()),
				"Note":($('#BusinessClientForm #Note').val())
			};
			var hasError=false;
			// Validation on required fields.
			$('#BusinessClientForm .RequiredField').each(function(){
				var field=$('#BusinessClientForm #'+$(this).attr('for'));
				$('#BusinessClientForm #'+field.attr('id')+'Message').empty();
				if(field.attr('id')=='AccountantContainer'){
					if($('#BusinessClientForm #AccountantContainer :radio:checked').val()===undefined){
						hasError=true;
						$('#BusinessClientForm #'+field.attr('id')+'Message').append('Required');
					}
				}else{
					var value=field.val();
					if(value===undefined||value==''){
						$('#BusinessClientForm #'+field.attr('id')+'Message').append('Required');
						hasError=true;
					}else{
						$('#BusinessClientForm #'+field.attr('id')+'Message').empty();
					}
				}
			});
			if(!hasError){
				$.ajax({
					url:"/SaveBusinessClient",
					type:"POST",
					dataType:"json",
					data:client,
					success:function(data,status,xhr){
						// After saving successfully, redirect back to client.jsp with ID.
						window.location.href='/GetClient?clientId='+data.clientId;
					},
					error:function(xhr,status,error){
						alert("error:"+error);
					}
				});
			}
		},
		Cancel:function(){
			$(this).dialog("close");
		}
	},
	open:function(){
		// If ID field is empty, then set all defaults.  If not, set all values.
		if($('#clientId').val()!=undefined){
			// Retrieve client info.
			// Get all users
			$.ajax({
				url:'/AjaxGetUsers',
				type:'GET',
				dataType:'json',
				success:function(data,status,xhr){
					var accountantContainer=$('#BusinessClientForm #AccountantContainer').empty();
					var users=data.data;
					var currentUserId='<%=((String)request.getAttribute("CurrentUserId"))%>';
					for(var i=0;i<users.length;i++){
						if(users[i].UserId==currentUserId){
							accountantContainer.append('<input id="'+users[i].UserId+'" name="Accountant" type="radio" value="'+users[i].UserId+'" checked="checked"/><label for="'+users[i].UserId+'">'+users[i].Nickname+'</label>');
						}else{
							accountantContainer.append('<input id="'+users[i].UserId+'" name="Accountant" type="radio" value="'+users[i].UserId+'"/><label for="'+users[i].UserId+'">'+users[i].Nickname+'</label>');
						}
					}
					accountantContainer.buttonset();
				},
				error:function(xhr,status,error){
					alert('Error:'+error);
				}
			});	
			$.ajax({
				url:'/AjaxGetClient',
				type:'GET',
				dataType:'json',
				data:{
					clientId:$('#clientId').val()
				},
				success:function(data,status,xhr){
					if(data.success==true){
						var client=data.data;
						$('#BusinessClientForm :input:text').each(function(index,element){
							$(this).val(client[$(this).attr('name')]);
						});
						$('#BusinessClientForm :input:radio').each(function(index,element){
							if(client[$(this).attr('name')]==$(this).attr('id')){
								$(this).attr('checked','checked').button('refresh');
							}
						});
						$('#BusinessClientForm :input:checkbox').each(function(index,element){
							if(client[$(this).attr('name')]!=undefined){
								if(client[$(this).attr('name')]=='true'){
									$(this).attr('checked','checked').button('refresh');
								}else{
									$(this).removeAttr('checked').button('refresh');
								}
							}
						});
						$('#BusinessClientForm #Note').append((client.Note!=undefined?client.Note:''));
					}else{
						alert('Error:'+data.error);
					}
				},
				error:function(xhr,status,error){
					alert('Error:'+error);
				}
			});
		}else{
			// Get all users
			$.ajax({
				url:'/AjaxGetUsers',
				type:'GET',
				dataType:'json',
				success:function(data,status,xhr){
					var accountantContainer=$('#BusinessClientForm #AccountantContainer').empty();
					var users=data.data;
					var currentUserId='<%=((String)request.getAttribute("CurrentUserId"))%>';
					for(var i=0;i<users.length;i++){
						if(users[i].UserId==currentUserId){
							accountantContainer.append('<input id="'+users[i].UserId+'" name="Accountant" type="radio" value="'+users[i].UserId+'" checked="checked"/><label for="'+users[i].UserId+'">'+users[i].Nickname+'</label>');
						}else{
							accountantContainer.append('<input id="'+users[i].UserId+'" name="Accountant" type="radio" value="'+users[i].UserId+'"/><label for="'+users[i].UserId+'">'+users[i].Nickname+'</label>');
						}
					}
					accountantContainer.buttonset();
				},
				error:function(xhr,status,error){
					alert('Error:'+error);
				}
			});	
		}
		// Clear all error messages
		$('#BusinessClientForm .Message').empty();
	},
	close:function(){
		// Clear all fields and reset defaults for radios.
		$('#BusinessClientForm :input[type="text"],textarea').val('');
		$('#BusinessClientForm #AccountantContainer').empty();
		// Clear all error messages
		$('#BusinessClientForm .Message').empty();
	}
	});
});
</script>
<div id="BusinessClientDialog">
<form id="BusinessClientForm">
<table>
	<tr>
		<td><label id="ClientNumberLabel" for="ClientNumber" class="RequiredField">Client No:</label></td><td>
		<input id="ClientNumber" name="ClientNumber" type="text" maxlength="30" size="30"/><label id="ClientNumberMessage" class="Message" for="ClientNumber"></label></td>
		<td></td><td></td>
	</tr>
	<tr>
		<td><label id="NameLabel" for="Name" class="RequiredField">Legal Name:</label></td><td><input id="Name" name="Name" type="text" maxlength="200" size="30"/><label id="NameMessage" class="Message" for="Name"></label></td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Business Name:</td><td><input id="BusinessName" name="BusinessName" type="text" maxlength="200" size="30"/></td>
		<td>Expiry Date:</td><td><input id="ExpiryDate" name="ExpiryDate" type="text"/></td>
	</tr>
	<tr>
		<td>Registered Address:</td><td><input id="RegisteredAddress" name="RegisteredAddress" type="text" maxlength="400" size="30"/></td>
		<td>Business Address:</td><td><input id="BusinessAddress" name="BusinessAddress" type="text" maxlength="400" size="30"/></td>
	</tr>
	<tr>
		<td>Mailing Address:</td><td><input id="MailingAddress" name="MailingAddress" type="text" maxlength="400" size="30"/></td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Contact Person:</td><td><input id="ContactPerson" name="ContactPerson" type="text" maxlength="80" size="30"/></td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Business Phone:</td><td><input id="BusinessPhone" name="BusinessPhone" type="text" maxlength="16" size="16"/></td>
		<td>Cell Phone:</td><td><input id="CellPhone" name="CellPhone" type="text" maxlength="16" size="16"/></td>
		<td>Home Phone:</td><td><input id="HomePhone" name="HomePhone" type="text" maxlength="16" size="16"/></td>
	</tr>
	<tr>
		<td>Fax:</td><td><input id="Fax" name="Fax" type="text" maxlength="16" size="16"/></td>
		<td>Email:</td><td><input id="Email" name="Email" type="text" maxlength="300" size="30"/></td>
	</tr>
	<tr>
		<td>Nature of Business:</td>
		<td>
			<div id="NatureOfBusinessRadio" style="float:left;">
				<input id="Corporation" name="NatureOfBusiness" type="radio"/><label for="Corporation">Corporation</label>
				<input id="Partnership" name="NatureOfBusiness" type="radio"/><label for="Partnership">Partnership</label>
				<input id="Sole-Proprietor" name="NatureOfBusiness" type="radio"/><label for="Sole-Proprietor">Sole-Proprietor</label>
			</div>
			<div id="FiscalYearEndContainer" style="float:left;"><label for="FiscalYearEnd">Fiscal Year End: </label><input id="FiscalYearEnd" name="FiscalYearEnd" type="text" style="width:115px;margin-top:5px;margin-bottom:5px;"/></div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Status:</td>
		<td>
			<div id="StatusRadio" style="float:left;">
				<input id="Active" name="Status" type="radio" checked="checked"/><label for="Active">Active</label>
				<input id="NoLongerOurClient" name="Status" type="radio"/><label for="NoLongerOurClient">No Longer Our Client</label>
			</div>
			<div id="EffectiveDateContainer" style="float:left;"><label for="EffectiveDate"> Effective Date: </label><input id="EffectiveDate" name="EffectiveDate" type="text" style="width:115px"/></div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Including:</td>
		<td>
			<div id="Including">
				<input id="Article" name="Article" type="checkbox"/><label for="Article">Article</label>
				<input id="Form1" name="Form1" type="checkbox"/><label for="Form1">Form1</label>
				<input id="HST" name="HST" type="checkbox"/><label for="HST">HST</label>
				<input id="Payroll" name="Payroll" type="checkbox"/><label for="Payroll">Payroll</label>
				<input id="Tax" name="Tax" type="checkbox"/><label for="Tax">Tax</label>
				<input id="ImportExport" name="ImportExport" type="checkbox"/><label for="ImportExport">Import/Export</label>
			</div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>Business No.:</td><td><input id="BusinessNumber" name="BusinessNumber" type="text" maxlength="30" size="30" style="float:left;"/>
			<div id="BusinessNumberContainer">
				<input id="BusinessNumberConsentForm" name="BusinessNumberConsentForm" type="checkbox"/>&nbsp;&nbsp;<label for="BusinessNumberConsentForm">Business Consent Form</label>
			</div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>WSIB No.:</td><td><input id="WSIBNumber" name="WSIBNumber" type="text" maxlength="30" size="30" style="float:left;"/>
			<div id="WSIBNumberContainer">
				<input id="WSIBNumberConsentForm" name="WSIBNumberConsentForm" type="checkbox"/>&nbsp;&nbsp;<label for="WSIBNumberConsentForm">Business Consent Form</label>
			</div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td>EHT No.:</td><td><input id="EHTNumber" name="EHTNumber" type="text" maxlength="30" size="30" style="float:left;"/>
			<div id="EHTNumberContainer">
				<input id="EHTNumberConsentForm" name="EHTNumberConsentForm" type="checkbox"/>&nbsp;&nbsp;<label for="EHTNumberConsentForm">Business Consent Form</label>
			</div>
		</td>
		<td></td><td></td>
	</tr>
	<tr>
		<td><label id="PASIDLabel" for="PASID" class="RequiredField">PAS ID:</label></td><td><input id="PASID" name="PASID" type="text" maxlength="30" size="30"/><label id="PASIDMessage" class="Message" for="PASID"></label></td>
	</tr>
	<tr>
		<td>Type of Services:</td>
		<td>
			<div id="TypeOfServicesContainer" style="float:left;">
				<input id="Monthly" name="TypeOfServices" type="radio"/><label for="Monthly">Monthly</label>
				<input id="Quarterly" name="TypeOfServices" type="radio"/><label for="Quarterly">Quarterly</label>
				<input id="Annually" name="TypeOfServices" type="radio"/><label for="Annually">Annually</label>
			</div>
		</td>
		<td>Pickup Date:</td><td>
		<input id="PickupDate" name="PickupDate"/>
		</td>
	</tr>
	<tr>
		<td><label for="AccountantContainer" id="AccountantLabel" class="RequiredField">Accountant:</label></td><td>
		<div id="AccountantContainer"></div><label id="AccountantContainerMessage" class="Message" for="AccountantContainer"></label>
		</td>
	</tr>
	<tr>
		<td>Note:</td><td><textarea id="Note" name="Note" rows="5" cols="60"></textarea></td>
		<td></td><td></td>
	</tr>
</table>
</form>
</div>